package com.monkey_pig.memoryalbum.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.Nullable;

import java.io.IOException;

/**
 * Created by Junerver on 2016/5/27.
 */
public class MyUtils {
    @Nullable
    public static String getFDateTime(String path) {
        String FDateTime=null;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            FDateTime = exifInterface .getAttribute(ExifInterface.TAG_DATETIME);

        } catch (IOException e) {
            e.printStackTrace();
        }
        if (FDateTime != null) {
            return FDateTime;
        } else {
            return "未知的神秘时间！";
        }
    }

    public static String fomartString(String date) {
        if (date!=null&&date!="未知的神秘时间！") {
            String subdate = date.substring(0, 7).trim();
            return subdate;
        }
        return "未知的神秘时间！";
    }

    //可以提取成工具类 通过uri获取文件真是路径
    public static String getRealFilePath(final Context context, final Uri uri) {
        if (null == uri) return null;
        final String scheme = uri.getScheme();
        String data = null;
        if (scheme == null)
            data = uri.getPath();
        else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
            data = uri.getPath();
        } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
            Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null);
            if (null != cursor) {
                if (cursor.moveToFirst()) {
                    int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                    if (index > -1) {
                        data = cursor.getString(index);
                    }
                }
                cursor.close();
            }
        }
        return data;
    }

}
